﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TheSubMatrixWithBiggestSum
{
    class Program
    {
        static void PrintMatrix(int[,] matrix)
        {
            for (int row = 0; row < matrix.GetLength(0); row++)
            {
                for (int column = 0; column < matrix.GetLength(1); column++)
                {
                    Console.Write(matrix[row, column] + "\t");
                }
                Console.WriteLine();
            }
        }

        /// <summary>
        /// finds the sum of a sub matrix
        /// </summary>
        /// <param name="matrix">the matrix</param>
        /// <param name="row">the index of the row of the middle element</param>
        /// <param name="column">the index of the column of the middle element</param>
        /// <returns>the sum of the sub matrix</returns>
        static int SubMatrixSum(int[,] matrix, int row, int column)
        {
            return matrix[row, column] + matrix[row - 1, column - 1] +
                matrix[row - 1, column] + matrix[row + 1, column] + matrix[row, column - 1] +  matrix[row,column + 1] 
                + matrix[row + 1,column + 1] +  matrix[row - 1,column + 1] +  matrix[row + 1,column - 1];           
        }

        static void Main(string[] args)
        {
            int numberOfRows = int.Parse(Console.ReadLine());
            int numberOfColumns = int.Parse(Console.ReadLine());
            int[,] theGreatMatrix = new int[numberOfRows, numberOfColumns];

            for (int row = 0; row < numberOfRows; row++)
            {
                for (int column = 0; column < numberOfColumns; column++)
                {
                    theGreatMatrix[row, column] = int.Parse(Console.ReadLine()); 
                }
            }

            int bestSum = int.MinValue;

            for (int row = 1; row < numberOfRows - 1; row++)
            {
                for (int column = 1; column < numberOfColumns - 1; column++)
                {
                    if (bestSum < SubMatrixSum(theGreatMatrix, row, column))
                    {
                        bestSum = SubMatrixSum(theGreatMatrix, row, column);
                    }
                }
            }
            Console.WriteLine();
            Console.WriteLine(bestSum);
        }
    }
}
